<?php

class qqapi{

	private $appid,$appkey,$callback,$access_token,$openid;

        public function __construct($appid, $appkey, $callback){
            $this->appid = $appid;
            $this->appkey = $appkey;
            $this->callback = $callback;
            $this->access_token= '';
            $this->openid = '';
        }

        public function redirect_to_login() {
            //跳转到QQ登录页的接口地址, 不要更改!!
            $redirect = "https://graph.qq.com/oauth2.0/authorize?response_type=code&client_id=$this->appid&scope=&redirect_uri=".rawurlencode($this->callback);
            //跳转到QQ登录页的接口地址, 不要更改!!
            $redirect = "https://graph.qq.com/oauth2.0/authorize?response_type=code&client_id={$this->appid}&redirect_uri={$this->callback}&state={$this->state}&scope=get_user_info";
            // $redirect = "http://xui.ptlogin2.qq.com/cgi-bin/xlogin?appid={$this->appid}&style=35&s_url=http://connect.qq.com&refer_cgi=authorize&which=&response_type=code&client_id={$this->appid}&redirect_uri={$this->callback}";

            // $redirect = "http://xui.ptlogin2.qq.com/cgi-bin/xlogin?appid=716027609&pt_3rd_aid={$this->appid}&style=35&s_url=http%3A%2F%2Fconnect.qq.com&refer_cgi=authorize&which=&response_type=code&client_id={$this->appid}&redirect_uri={$this->callback}";
            header("Location:$redirect");
        }
        //获得登录的 openid
        public function get_openid($code){
            $url = "https://graph.qq.com/oauth2.0/token?grant_type=authorization_code&client_id={$this->appid}&client_secret={$this->appkey}&code={$code}&redirect_uri=".$this->callback;
            $content = $this->vGet($url);
            parse_str($content, $arr);
            if (!empty($arr['access_token'])) {
                $this->access_token=$arr['access_token'];
                $url="https://graph.qq.com/oauth2.0/me?access_token={$this->access_token}";
                $content = $this->vGet($url);
                $content = str_replace('callback( ','',$content);
                $content = str_replace(' );','',$content);
                $returns = json_decode($content);
                if (!empty($returns->openid)) {
                    return $returns->openid;
                } else {
                    return false;
                }
            } else {
                return false;
            }
        }
        /**
        * 返回用户信息
        * 
        */
        public function get_user_info(){
            $url = "https://graph.qq.com/user/get_user_info?access_token=$this->access_token&oauth_consumer_key=$this->appid&openid=$this->openid";
            $content=file_get_contents($url);
            $result = json_decode($content);
            return $result->nickname;
        }
    /**
     * 邮件发送
     *
     * @param: $url[string]        请求地址
     * @param: $data[json]       请求数据
     *
     * @return $tmpInfo 数据
     */
    public function vGet($url){ // 模拟提交数据函数
        $curl = curl_init(); // 启动一个CURL会话
        curl_setopt($curl, CURLOPT_URL, $url); // 要访问的地址
        curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE); // 对认证证书来源的检查
        curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, FALSE); // 从证书中检查SSL加密算法是否存在
        curl_setopt($curl, CURLOPT_USERAGENT, 'Mozilla/5.0 (compatible; MSIE 5.01; Windows NT 5.0)'); // 模拟用户使用的浏览器
        // curl_setopt($curl, CURLOPT_FOLLOWLOCATION, 1); // 使用自动跳转
        // curl_setopt($curl, CURLOPT_AUTOREFERER, 1); // 自动设置Referer
        curl_setopt($curl, CURLOPT_TIMEOUT, 30); // 设置超时限制防止死循环
        curl_setopt($curl, CURLOPT_HEADER, 0); // 显示返回的Header区域内容
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1); // 获取的信息以文件流的形式返回
        $tmpInfo = curl_exec($curl); // 执行操作
        if (curl_errno($curl)) {
           echo 'Errno'.curl_error($curl);//捕抓异常
        }
        curl_close($curl); // 关闭CURL会话
        return $tmpInfo; // 返回数据
    }
}
?>